#include "stdafx.h"
#include "Block.h"

CBlock::CBlock(CWnd* pParent,const LONG & Size_X,const LONG & Size_Y, const int & ID,const LONG & Pos_X,const LONG & Pos_Y, bool bIsMine, bool bWasOpened, bool bWasChecked, int iCareState )
	: m_bIsMine(bIsMine), m_bIsOpened(bWasOpened), m_iCareState(iCareState), m_bLRClicked(false), m_bLPress(false), m_bRPress(false), m_hParent(pParent), m_uId(ID)
{
	Create(_T(""), WS_CHILD|BS_PUSHBUTTON, CRect(0, 0, Size_X, Size_Y), pParent, ID);

	MakePosition( &CPoint( Pos_X , Pos_Y ) , NULL );
	SetImage(IDB_NORMAL);
}

CBlock::~CBlock(){

}

void CBlock::MakeInitialization(){
	m_bIsOpened		=	false;
	m_bIsMine		=	false;
	m_bLRClicked	=	false;
	m_bLPress		=	false;
	m_bRPress		=	false;
	m_iCareState	=	CS_NONE;
	SetImage(IDB_NORMAL);
}

BEGIN_MESSAGE_MAP(CBlock, CMFCButton)
	ON_WM_RBUTTONDOWN()
	ON_WM_RBUTTONUP()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
END_MESSAGE_MAP()

void CBlock::MakePosition( CPoint* LPPos , CSize* LPSize ){
	UINT nFlag;
	CWnd* pButton;
	if( !(LPPos || LPSize) )
		return;
	pButton = m_hParent->GetDlgItem(m_uId);
	if( LPSize ){
		nFlag = SWP_NOMOVE|SWP_NOZORDER;
		pButton->SetWindowPos(NULL, 0 , 0 , LPSize->cx , LPSize->cy , nFlag);
	}
	if( LPPos ){
		nFlag = SWP_NOSIZE|SWP_NOZORDER;
		pButton->SetWindowPos(NULL, LPPos->x , LPPos->y , 0 , 0 , nFlag);
	}
}

void CBlock::MakeVisable( bool visable )
{
	m_bVisable = visable;
	if( visable )
		ShowWindow( SW_SHOW );
	else
		ShowWindow( SW_HIDE );
}

void CBlock::OnRButtonDown(UINT nFlags, CPoint point)
{
	CWnd* pParent = GetParent();
	if (pParent != NULL)
		pParent->SendMessage( WM_COMMAND, MAKEWPARAM(GetDlgCtrlID(), CHECKRDOWN), (LPARAM) m_hWnd);

	m_bRPress = true;

	if (m_bLPress)
	{
		m_bLRClicked = true;
		if (pParent != NULL)
		{
			pParent->SendMessage( WM_COMMAND, MAKEWPARAM(GetDlgCtrlID(), WM_LRBUTTONBOTHDOWN), (LPARAM) m_hWnd);
		}
	}

	CMFCButton::OnRButtonDown(nFlags, point);
}


void CBlock::OnRButtonUp(UINT nFlags, CPoint point)
{
	m_bRPress = false;

	CWnd* pParent = GetParent();

	if (m_bLPress)
		return;
	else if (m_bLRClicked)
	{
		if (pParent != NULL)
		{
			pParent->SendMessage( WM_COMMAND, MAKEWPARAM(GetDlgCtrlID(), WM_LRBUTTONBOTHDCLICKED), (LPARAM) m_hWnd);
			m_bLRClicked = false;
		}
		return;
	}

	if (pParent != NULL)
	{
		pParent->SendMessage( WM_COMMAND, MAKEWPARAM(GetDlgCtrlID(), WM_RBUTTONUP), (LPARAM) m_hWnd);
	}

	CMFCButton::OnRButtonUp(nFlags, point);
}

void CBlock::OnLButtonDown(UINT nFlags, CPoint point)
{
	CWnd* pParent = GetParent();
	if (pParent != NULL)
	{
		pParent->SendMessage( WM_COMMAND, MAKEWPARAM(GetDlgCtrlID(), CHECKLDOWN), (LPARAM) m_hWnd);
	}

	m_bLPress = true;

	if (m_bRPress)
	{
		m_bLRClicked = true;
		if (pParent != NULL)
		{
			pParent->SendMessage( WM_COMMAND, MAKEWPARAM(GetDlgCtrlID(), WM_LRBUTTONBOTHDOWN), (LPARAM) m_hWnd);
		}
	}
	else
	{
		if (pParent != NULL)
		{
			pParent->SendMessage( WM_COMMAND, MAKEWPARAM(GetDlgCtrlID(), WM_LBUTTONDOWN), (LPARAM) m_hWnd);
		}
	}

	CMFCButton::OnLButtonDown(nFlags, point);
}


void CBlock::OnLButtonUp(UINT nFlags, CPoint point)
{
	m_bLPress = false;

	CWnd* pParent = GetParent();

	if (!m_bLRClicked)
	{
		if (pParent != NULL)
		{
			pParent->SendMessage( WM_COMMAND, MAKEWPARAM(GetDlgCtrlID(), WM_LBUTTONUP), (LPARAM) m_hWnd);
		}
	}

	if (m_bRPress)
		return;
	else if (m_bLRClicked)
	{
		if (pParent != NULL)
		{
			pParent->SendMessage( WM_COMMAND, MAKEWPARAM(GetDlgCtrlID(), WM_LRBUTTONBOTHDCLICKED), (LPARAM) m_hWnd);
			m_bLRClicked = false;
		}
		return;
	}

	CMFCButton::OnLButtonUp(nFlags, point);
}

// void CBlock::OnRButtonDblClk(UINT nFlags, CPoint point)
// {
// 	CWnd* pParent = GetParent();
// 	if (pParent != NULL)
// 	{
// 		pParent->SendMessage( WM_COMMAND, MAKEWPARAM(GetDlgCtrlID(), WM_RBUTTONDBLCLK), (LPARAM) m_hWnd);
// 	}
// 
// 	CMFCButton::OnRButtonUp(nFlags, point);
// }
